<?php
	class image extends page {
		public $layout = false;
		private $image;	
		private $url = 'http://thetvdb.com/banners/';
		private $opt;
		private $id;
		private $img;
		
		public function load(){		
			$this->opt = request_var('value1',false);
			$this->id = request_var('value2',false);
			$width = $resize = request_var('value3', false);
			$height = false;

			if(stripos($resize, 'x'))
			{
				list($width, $height) = explode('x', $resize);
			}

			if(!$this->id) 
				return false;

			$this->image = TMP_DIR . $this->id;
			$this->createImgIfNotExists();

			if(!file_exists($this->image))
				return false;

			$expires = 60*60*24*365;

			header("Pragma: public");
			header("Cache-Control: maxage=".$expires);
			header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
			header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
			header('Content-type: ' . mime_content_type($this->image) . ';');
			header('Accept-Ranges: bytes');
			
		
			$this->openImage($width, $height);
				
		}
		
		private function createImgIfNotExists(){
			
			if(!file_exists($this->image))	{
				if($name = thetvdb::getImageNameForMD5($this->opt, $this->id)){			
					$this->url .= $name;

					getURLContent($this->url, $this->image);
				}
			}
		}
		
		private function openImage($width = false, $height = false){	
			if(!$siz = @getimagesize($this->image)){
				@unlink($this->image);
			}

			switch($siz[2]){
				case IMG_PNG:
					$this->img = @imagecreatefrompng($this->image);
				break;
				
				case IMG_JPG:
					$this->img = @imagecreatefromjpeg($this->image);
				break;

				case IMG_GIF:
					$this->img = @imagecreatefromjpeg($this->image);
				break;
				
				default: 
					$this->img = false;
				break;
			}

			if(!$this->img){
				@unlink($this->image);
				header('Content-type: image/jpeg;');
				$this->img = imagecreate(1,1);
				imagejpeg($this->img, null, 100);
				return false;
			}
			
			if($width && $height)
				return $this->resize_then_crop($width, $height);
			
			if(intval($width) != 0)
			{
				$newwidth = $width;
				$newheight = $siz[1] * ($width / $siz[0]);
				$image_p = imagecreatetruecolor($newwidth, $newheight);
				imagecopyresampled($image_p, $this->img, 0, 0, 0, 0, $newwidth, $newheight, $siz[0], $siz[1]);
				imagedestroy($this->img);
				$this->img = $image_p;
			
			} 
			
			
			imagejpeg($this->img, null, 100);
			imagedestroy($this->img);
			
		}
		private function resize_then_crop($imagethumbsize_w, $imagethumbsize_h, $red = 255, $green = 255, $blue = 255)
		{
			$width = $imagethumbsize_w ;
			$height = $imagethumbsize_h ;
			list($width_orig, $height_orig) = getimagesize($this->image);

			if ($width_orig < $height_orig) {
			  $height = ($imagethumbsize_w / $width_orig) * $height_orig;
			} else {
			    $width = ($imagethumbsize_h / $height_orig) * $width_orig;
			}

			if ($width < $imagethumbsize_w)
			//if the width is smaller than supplied thumbnail size 
			{
			$width = $imagethumbsize_w;
			$height = ($imagethumbsize_w/ $width_orig) * $height_orig;
			}

			if ($height < $imagethumbsize_h)
			//if the height is smaller than supplied thumbnail size 
			{
			$height = $imagethumbsize_h;
			$width = ($imagethumbsize_h / $height_orig) * $width_orig;
			}

			$thumb = imagecreatetruecolor($width, $height);  
			$bgcolor = imagecolorallocate($thumb, $red, $green, $blue);   
			ImageFilledRectangle($thumb, 0, 0, $width, $height, $bgcolor);
			imagealphablending($thumb, true);

			imagecopyresampled($thumb, $this->img, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
			imagedestroy($this->img);
			$this->img = imagecreatetruecolor($imagethumbsize_w , $imagethumbsize_h);
			// true color for best quality
			$bgcolor = imagecolorallocate($this->img, $red, $green, $blue);   
			imagefilledrectangle($this->img, 0, 0, $imagethumbsize_w , $imagethumbsize_h , $bgcolor);
			imagealphablending($this->img, true);

			$w1 =($width/2) - ($imagethumbsize_w/2);
			$h1 = ($height/2) - ($imagethumbsize_h/2);

			imagecopyresampled($this->img, $thumb, 0,0, $w1, $h1,
			$imagethumbsize_w , $imagethumbsize_h ,$imagethumbsize_w, $imagethumbsize_h);

			imagejpeg($this->img, null, 100);
			imagedestroy($this->img);
			imagedestroy($thumb);
		}
	}

?>